<template>
  <view class="content">
    <view class="form-box">
      <view class="title">派颂乐商户端</view>
      <view class="row-input">
        <input type="text" placeholder="用户名" maxlength="18" v-model="userName" />
      </view>
      <view class="row-input">
        <input type="password" placeholder="密码" maxlength="16" v-model="password" />
      </view>
      <view class="btn-box">
        <view class="btn login" @click="handleLogin">登录</view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { loginAsMerchant } from '@/api/admin-api.js';

const userName = ref('');
const password = ref('');

// 从本地存储读取账号列表
onMounted(() => {
  const savedAccount = uni.getStorageSync('account') || '';
  if (savedAccount !== '') {
    userName.value = savedAccount;
  }
});

const handleAccountChange = (e) => {
  selectedAccount.value = e.detail.value;
  userName.value = e.detail.value;
};

const handleLogin = async () => {
  if (!userName.value || !password.value) {
    uni.showToast({
      title: '请输入用户名和密码',
      icon: 'none'
    });
    return;
  }

  try {
    const response = await loginAsMerchant(userName.value, password.value);
	console.log(response)
    if (response && response.merchant_id) {
      // 计算一月后的日期
      const oneMonthLater = new Date();
      oneMonthLater.setMonth(oneMonthLater.getMonth() + 1);

      // 保存登录状态和用户信息到本地存储
      const merchantInfo = {
        merchantId: response.merchant_id,
		realName: response.real_name,
        expirationDate: oneMonthLater.toISOString()
      };

      uni.setStorageSync('merchantInfo', merchantInfo);

      // 保存当前账号到本地存储
      uni.setStorageSync('account', userName.value);

      uni.showToast({
        title: '登录成功',
        duration: 2000
      });

      // 登录成功后跳转到首页或其他页面
      uni.switchTab({
        url: `/pages/index/index`
      });
    }
  } catch (error) {
    uni.showToast({
      title: error.user_message,
      icon: 'none'
    });
  }
};
</script>

<style lang="scss" scoped>
.content {
  position: relative;
  width: 100%;
  height: 100%;
}

.form-box {
  position: fixed;
  background: inherit;
  top: 25vh;
  left: 50rpx;
  right: 50rpx;
  padding: 60rpx 60rpx 0rpx 60rpx;
  border-radius: 30rpx;
  background-color: #9faa9f;

  .title {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100rpx;
    letter-spacing: 6rpx;
    font-size: 60rpx;
    color: #FFFFFF;
    font-weight: bold;
  }

  .row-input {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 40rpx 0rpx;
    height: 100rpx;
    font-size: 28rpx;
    padding: 0 20rpx;
    background-color: #FFFFFF;
    border-radius: 10rpx;
  }

  .btn-box {
    padding: 20rpx 0rpx 60rpx 0rpx;

    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100rpx;
      border-radius: 10rpx;
      font-size: 35rpx;
      letter-spacing: 5rpx;
    }

    .login {
      color: #FFFFFF;
      background-color: #333333;
    }
  }
}
</style>